package com.ipharma.config;

import java.io.FileInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Lee un archivo xml, donde estan los parametros de configurtacion del sistema
 * y los almacena en variables de instancia.
 * 
 * @author afortunato
 */
public class Config {

	private String operationMode;

	private String asignmentMode;

	private String ticketGeneration;

	private String timeout;

	private String userIdGeneration;

	private String checkPrints;

	private String ticketTitle;

	private String printerPort;

	private String fingerPrintPort;

	private String institucion;

	private String maxProdOnBasket;

	private String ticketWidth;

	private String dispenser;

	private int assignmentPaciente;

	private int assignmentSector;

	private int assignmentHabitacion;

	private int assignmentCama;

	private int identificadorEquipo;

	public Config(String filename) throws IOException {

		parseConfig(new FileInputStream(filename));
	}

	private void parseConfig(FileInputStream f) throws IOException {

		String ls_line;
		int li_pos;

		try {

			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			docBuilderFactory.setValidating(false);

			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(f);

			// language
			Element element = getElementById(doc, "parameter");

			operationMode = getAttribute(element, "operationMode");
			asignmentMode = getAttribute(element, "asignmentMode");
			ticketGeneration = getAttribute(element, "ticketGeneration");
			timeout = getAttribute(element, "timeout");
			userIdGeneration = getAttribute(element, "userIdGeneration");
			checkPrints = getAttribute(element, "checkPrints");
			ticketTitle = getAttribute(element, "ticketTitle");
			printerPort = getAttribute(element, "printerPort");
			fingerPrintPort = getAttribute(element, "fingerPrintPort");
			institucion = getAttribute(element, "institucion");
			maxProdOnBasket = getAttribute(element, "maxProdOnBasket");
			ticketWidth = getAttribute(element, "ticketWidth");
			dispenser = getAttribute(element, "dispenser");
			identificadorEquipo = new Integer(getAttribute(element,
			"identificadorEquipo")).intValue();
			assignmentPaciente = new Integer(getAttribute(element,
					"assignmentPaciente")).intValue();
			assignmentCama = new Integer(
					getAttribute(element, "assignmentCama")).intValue();
			assignmentHabitacion = new Integer(getAttribute(element,
					"assignmentHabitacion")).intValue();
			assignmentSector = new Integer(getAttribute(element,
					"assignmentSector")).intValue();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getOperationMode() {

		return operationMode;
	}

	public void setOperationMode(String operationMode) {

		this.operationMode = operationMode;
	}

	private Element getElementById(Document document, String key) {
		NodeList nodes = document.getElementsByTagName(key);
		return (Element) nodes.item(0);
	}

	private String getAttribute(Element element, String keyString) {
		if (keyString == null || element == null) {
			throw new NullPointerException(
					"Null key/element while reading configuration file");
		}

		String _retString = element.getAttribute(keyString);
		if (_retString == null) {
			throw new NullPointerException(
					"Null value while reading configuration file.  Element: "
							+ element.getTagName() + "; attribute: "
							+ keyString + " .");
		}
		return _retString;
	}

	public String getAsignmentMode() {

		return asignmentMode;
	}

	public void setAsignmentMode(String asignmentMode) {

		this.asignmentMode = asignmentMode;
	}

	public String getCheckPrints() {

		return checkPrints;
	}

	public void setCheckPrints(String checkPrints) {

		this.checkPrints = checkPrints;
	}

	public String getFingerPrintPort() {

		return fingerPrintPort;
	}

	public void setFingerPrintPort(String fingerPrintPort) {

		this.fingerPrintPort = fingerPrintPort;
	}

	public String getInstitucion() {

		return institucion;
	}

	public void setInstitucion(String institucion) {

		this.institucion = institucion;
	}

	public String getMaxProdOnBasket() {

		return maxProdOnBasket;
	}

	public void setMaxProdOnBasket(String maxProdOnBasket) {

		this.maxProdOnBasket = maxProdOnBasket;
	}

	public String getPrinterPort() {

		return printerPort;
	}

	public void setPrinterPort(String printerPort) {

		this.printerPort = printerPort;
	}

	public String getTicketGeneration() {

		return ticketGeneration;
	}

	public void setTicketGeneration(String ticketGeneration) {

		this.ticketGeneration = ticketGeneration;
	}

	public String getTicketTitle() {

		return ticketTitle;
	}

	public void setTicketTitle(String ticketTitle) {

		this.ticketTitle = ticketTitle;
	}

	public String getTicketWidth() {

		return ticketWidth;
	}

	public void setTicketWidth(String ticketWidth) {

		this.ticketWidth = ticketWidth;
	}

	public String getTimeout() {

		return timeout;
	}

	public void setTimeout(String timeout) {

		this.timeout = timeout;
	}

	public String getUserIdGeneration() {

		return userIdGeneration;
	}

	public void setUserIdGeneration(String userIdGeneration) {

		this.userIdGeneration = userIdGeneration;
	}

	public String getDispenser() {

		return dispenser;
	}

	public void setDispenser(String dispenser) {

		this.dispenser = dispenser;
	}

	public int getAssignmentPaciente() {

		return assignmentPaciente;
	}

	public void setAssignmentPaciente(int assignmentPaciente) {

		this.assignmentPaciente = assignmentPaciente;
	}

	public int getAssignmentCama() {

		return assignmentCama;
	}

	public void setAssignmentCama(int assignmentCama) {

		this.assignmentCama = assignmentCama;
	}

	public int getAssignmentHabitacion() {

		return assignmentHabitacion;
	}

	public void setAssignmentHabitacion(int assignmentHabitacion) {

		this.assignmentHabitacion = assignmentHabitacion;
	}

	public int getAssignmentSector() {

		return assignmentSector;
	}

	public void setAssignmentSector(int assignmentSector) {

		this.assignmentSector = assignmentSector;
	}

	public int getIdentificadorEquipo() {
		return identificadorEquipo;
	}

	public void setIdentificadorEquipo(int identificadorEquipo) {
		this.identificadorEquipo = identificadorEquipo;
	}

}
